package com.java46.carera.stock.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.carera.base.entity.PutStorage;
import com.java46.carera.base.entity.Repertory;
import com.java46.carera.base.exception.BusinessException;
import com.java46.carera.base.mapper.RepertoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName StockService
 * @Description TODO
 * @Author Administrator
 * @Date 2018/8/22 0022 10:00
 * @Vcrsion 1.0
 **/
@Service
public class StockService {

    @Autowired
    private RepertoryMapper repertoryMapper;


    /**
     * @Author xiao
     * @Description //分页查询库存可根据关键字
     * @Date 10:37 2018/8/22 0022
     * @Param [pageNum, pageSize, keywords]
     * @return com.github.pagehelper.PageInfo<com.java46.carera.base.entity.Repertory>
     */
    public PageInfo<Repertory> queryStock(int pageNum,int pageSize,String keywords) throws Exception {

        PageHelper.startPage(pageNum,pageSize);
        List<Repertory> repertories = repertoryMapper.selectAll(keywords);
        if (repertories.isEmpty()){
            throw new BusinessException("没有查询到信息");
        }
        PageInfo<Repertory> pages = new PageInfo<>(repertories);
        return pages;
    }

    /**
     * @Author xiao
     * @Description //新增配件
     * @Date 19:31 2018/8/22 0022
     * @Param [repertory]
     * @return void
     */
    public void insertStock(Repertory repertory) throws Exception {

        if(repertory==null){
            throw new BusinessException("没有任何配件信息");
        }
        repertoryMapper.insertRepertory(repertory);
    }

    /**
     * @Author xiao
     * @Description //通过库存编号删除库存记录
     * @Date 12:42 2018/8/23 0023
     * @Param [repid]
     * @return void
     */
    public void deleteByRepId(Integer repid) throws Exception {

        if(repid==null){
            throw new BusinessException("库存记录信息为空");
        }
        repertoryMapper.deleteByRepid(repid);
    }

    /**
     * @Author xiao
     * @Description //通过库存编号修改库存记录
     * @Date 16:25 2018/8/23 0023
     * @Param [repertory]
     * @return void
     */
    public void updateStock(Repertory repertory) throws Exception {

        if(repertory==null){
            throw new BusinessException("库存记录信息为空");
        }
        repertoryMapper.updateByRepid(repertory);
    }

    /**
     * @Author xiao
     * @Description //通过库存编号查询库存记录
     * @Date 16:35 2018/8/23 0023
     * @Param [repid]
     * @return com.java46.carera.base.entity.Repertory
     */
    public Repertory queryStockByRepId(Integer repid) throws Exception {

        if(repid==null){
            throw new BusinessException("库存记录信息为空");
        }
        Repertory repertory = repertoryMapper.selectByRepid(repid);
        return repertory;
    }

}
